package com.lzk.basic.controller;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.service.IVerifyCodeService;
import com.lzk.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码接口
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeService.getVerifyCode(key);
            return JsonResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {//业务异常捕获
            e.printStackTrace();
            return JsonResult.me(false,e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me(false,"系统异常，请稍后重试");
        }
    }

    /**
     * 获取手机验证码接口
     * @param map
     * @return
     */
    @PostMapping("/smsCode")
    public JsonResult sendSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.sendSmsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {//业务异常捕获
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me(false,"系统异常，请稍后重试");
        }
    }

    /**
     * 微信绑定手机号验证码接口
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
           return JsonResult.me();
        }  catch (BusinessException e) {//业务异常捕获
            e.printStackTrace();
            return JsonResult.me(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me(false,"系统异常，请稍后重试");
        }
    }
}
